package com.licw.service.impl;

import com.licw.context.BaseContext;
import com.licw.dto.ShoppingCartDTO;
import com.licw.entity.Dish;
import com.licw.entity.Setmeal;
import com.licw.entity.ShoppingCart;
import com.licw.mapper.DishMapper;
import com.licw.mapper.SetmealMapper;
import com.licw.mapper.ShoppingCartMapper;
import com.licw.service.ShoppingCartService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Resource
    private ShoppingCartMapper shoppingCartMapper;
    @Resource
    private DishMapper dishMapper;
    @Resource
    private SetmealMapper setmealMapper;
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();

        // 只能查询自己的购物车数据
        shoppingCart.setUserId(BaseContext.getCurrentId());
        // 设置新增商品
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        // 查询商品是否存在
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        // 商品存在则数量+1
        if(CollectionUtils.isNotEmpty(shoppingCartList)){
            shoppingCart = shoppingCartList.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartMapper.updateNumberById(shoppingCart);
            return;
        }
        // 商品不存在
        //判断当前添加到购物车的是菜品还是套餐
        if (shoppingCartDTO.getDishId() != null) {
            //添加到购物车的是菜品
            Dish dish = dishMapper.getById(shoppingCartDTO.getDishId());
            shoppingCart.setName(dish.getName());
            shoppingCart.setImage(dish.getImage());
            shoppingCart.setAmount(dish.getPrice());
        } else {
            //添加到购物车的是套餐
            Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
            shoppingCart.setName(setmeal.getName());
            shoppingCart.setImage(setmeal.getImage());
            shoppingCart.setAmount(setmeal.getPrice());
        }
        shoppingCart.setNumber(1);
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCartMapper.insert(shoppingCart);
    }

    @Override
    public List<ShoppingCart> list() {
        // 只能查询自己的购物车数据
        Long currentId = BaseContext.getCurrentId();
        return shoppingCartMapper.listByUserId(currentId);
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        // 只能查询自己的购物车数据
        shoppingCart.setUserId(BaseContext.getCurrentId());
        // 设置待删除的商品
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        // 查询商品
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);

        // 如果商品不存在
        if(CollectionUtils.isEmpty(shoppingCartList))return;

        shoppingCart = shoppingCartList.get(0);
        // 商品存在且数量大于1，则-1
        if(shoppingCart.getNumber() > 1){
            shoppingCart.setNumber(shoppingCart.getNumber() - 1);
            shoppingCartMapper.updateNumberById(shoppingCart);
        }
        // 商品存在且数量等于1，则删除
        else {
            shoppingCartMapper.delete(shoppingCart);
        }
    }

    @Override
    public void deleteAll() {
        Long currentId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteByUserId(currentId);
    }
}
